﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Fortuitus.Core;

namespace Fortuitus.Utilities
{
    public static class VectorHelper
    {
        public static IVector LoadVector(string line, string separator = "\t", int numberOfHeader = 0)
        {
            string[] array = line.Split("\t".ToCharArray());
            List<double> numbers = new List<double>();

            for (int i = numberOfHeader; i < array.Length; i++)
            {
                numbers.Add(double.Parse(array[i]));
            }

            EditableVector v = new EditableVector(numbers);
            for (int i = 0; i < numberOfHeader; i++)
            {
                v.SetValue<string>("header_" + i, array[i].Trim());
            }
            return v;
        }

        public static IOrderedList<IVector> LoadVectors(IEnumerable<string> lines, string colSeparator = "\t", int numberOfRowHeader = 0)
        {
            
            OrderedList<IVector> ret = new OrderedList<IVector>();
            foreach (var line in lines)
            {
                ret.Add(LoadVector(line, colSeparator, numberOfRowHeader));

            }

            return ret;
        }

        public static Tuple<IVectorView, IVectorView> PairwiseDeletionView(IVector vector1, IVector vector2)
        {
            Tuple<IVectorView, IVectorView> ret;
            List<int> indxMask = new List<int>();
            int minLength = Math.Min(vector1.Length, vector1.Length);

            for (int i = 0; i < minLength; i++)
            {
                if (!double.IsNaN(vector1[i]) && !double.IsNaN(vector1[i]))
                {
                    indxMask.Add(i);
                }
            }
            ret = new Tuple<IVectorView, IVectorView>(new MaskedVectorView(vector1, indxMask.ToArray()), new MaskedVectorView(vector1, indxMask.ToArray()));
            return ret;
        }

    }
}
